<?php


namespace app\admin\controller;

use think\Db;
use think\Validate;

/**
 * 区域列表
 * Class Region
 * @package app\admin\controller
 */
class Region extends Base
{

    /**
     * 城市列表
     */
    public function city_list()
    {
        $region_id = I('region_id/d', 0);
        $where = [];
        if ($region_id) {
            $where['region_id'] = $region_id;
        }
        $db = Db::name('city')->where(['status' => 1])->where($where)->select();
        $this->returnMsg('1', '', $db);
    }

    /**
     * 城市详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function city_msg()
    {
        $id = input('id',0);
        $db = Db::name('city')->where(['id'=>$id,'status'=>1])->find();
        $this->returnMsg('1', '', $db);
    }

    /**
     * 添加城市
     */
    public function add_city()
    {
//        echo phpinfo();
//        exit();

        $add_data = I('.');
        unset($add_data['token']);
        $rule = [
            'names' => 'require|length:1,50',
            'names_e' => 'require|length:1,200',
            'region_id' => 'require|number',
        ];

        $msg = [
            'names.require' => '请输入城市名字',
            'names.length' => '请输入1-50字的城市名字',
            'names_e.require' => '请输入城市英文名字',
            'names_e.length' => '请输入1-100字的城市英文名字',
            'region_id.require' => '请选择城市区域',
            'region_id.number' => '城市区域格式错误'
        ];

//        var_dump(json_encode($add_data));
        $validate = new Validate($rule, $msg);
        $result = $validate->check($add_data);
        if (!$result) {
            $this->returnMsg(-1, $validate->getError());
        }

        $names = $add_data['names'];
        $have_db = Db::name('city')->where(['names' => $names,'status'=>1])->find();
        if (!empty($have_db)) {
            $this->returnMsg(-1, '已有相同的城市', []);
        }

        $db = Db::name('city')->insert($add_data);
        $this->returnMsg(1, '添加成功', $db);
    }

    /**
     * 编辑城市
     */
    public function edit_city()
    {
        $add_data = I('.');
        unset($add_data['token']);

        $rule = [
            'id' => 'require|number',
            'names' => 'require|length:1,50',
            'names_e' => 'require|length:1,200',
            'region_id' => 'require|number',
        ];

        $msg = [
            'id.require' => '请选择城市',
            'id.number' => '请选择城市编号',
            'names.require' => '请输入城市名字',
            'names.length' => '请输入1-50字的城市名字',
            'names_e.require' => '请输入城市英文名字',
            'names_e.length' => '请输入1-100字的城市英文名字',
            'region_id.require' => '请选择城市区域',
            'region_id.number' => '城市区域格式错误'
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($add_data);
        if (!$result) {
            $this->returnMsg(-1, $validate->getError());
        }

        $cid = $add_data['id'];

        $city_db = Db::name('city')->where(['id' => $cid,'status'=>1])->find();
        if(!$city_db) {
            $this->returnMsg(-1, '没有找到城市数据', []);
        }

        $names = $add_data['names'];
        $have_db = Db::name('city')->where(['names' => $names,'status'=>1, 'id' => ['neq', $cid]])->find();
        if (!empty($have_db)) {
            $this->returnMsg(-1, '已有相同的城市名称', []);
        }

        $db = Db::name('city')->where(['id' => $cid])->update($add_data);
        $this->returnMsg(1, '修改成功', $db);
    }

    /**
     * 删除城市
     */
    public function delete_city()
    {
        $cid = input('id');
        $storehouses = Db::name('storehouse')->where(['city_id' => $cid, 'status' => 1])->find();
        if ($storehouses) {
            $this->returnMsg(-1, '已有仓库在当前城市');
        }

//        Db::name('city')->where(['id' => $cid])->delete();
        Db::name('city')->where(['id' => $cid])->update(['status' => 2]);
        $this->returnMsg(1, '删除成功');
    }

    /**
     * 区域列表
     */
    public function region_list(){
        $db = Db::name('region')->where(['status' => 1])->select();
        $this->returnMsg('1', '', $db);
    }

    /**
     * 区域详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function region_msg()
    {
        $id = input('id',0);
        $db = Db::name('region')->where(['id'=>$id,'status' => 1])->find();
        $this->returnMsg('1', '', $db);
    }

    /**
     * 添加区域
     */
    public function add_region(){
        $add_data = I('.');
        unset($add_data['token']);

        $rule = [
            'names' => 'require|length:1,50',
            'names_e' => 'require|length:1,200',
        ];

        $msg = [
            'names.require' => '请输入区域名字',
            'names.length' => '请输入1-50字的区域名字',
            'names_e.require' => '请输入区域英文名字',
            'names_e.length' => '请输入1-100字的区域英文名字',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($add_data);
        if (!$result) {
            $this->returnMsg(-1, $validate->getError());
        }

        $names = $add_data['names'];
        $have_db = Db::name('region')->where(['names' => $names,'status'=>1])->find();
        if (!empty($have_db)) {
            $this->returnMsg(-1, '已有相同的区域', []);
        }

        $db = Db::name('region')->insert($add_data);
        $this->returnMsg(1, '添加成功', $db);
    }

    /**
     * 编辑区域
     */
    public function edit_region(){
        $add_data = I('.');
        unset($add_data['token']);

        $rule = [
            'id' => 'require|number',
            'names' => 'require|length:1,50',
            'names_e' => 'require|length:1,200',
        ];

        $msg = [
            'id.require' => '请选择区域编号',
            'id.number' => '请选择区域编号。',
            'names.require' => '请输入区域名字',
            'names.length' => '请输入1-50字的区域名字',
            'names_e.require' => '请输入区域英文名字',
            'names_e.length' => '请输入1-100字的区域英文名字',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($add_data);
        if (!$result) {
            $this->returnMsg(-1, $validate->getError());
        }

        $cid = $add_data['id'];

        $region_db = Db::name('region')->where(['id' => $cid,'status'=>1])->find();
        if(!$region_db) {
            $this->returnMsg(-1, '没有找到区域数据', []);
        }

        $names = $add_data['names'];
        $have_db = Db::name('region')->where(['names' => $names,'status'=>1, 'id' => ['neq', $cid]])->find();
        if (!empty($have_db)) {
            $this->returnMsg(-1, '已有相同的区域名称', []);
        }

        $db = Db::name('region')->where(['id' => $cid])->update($add_data);
        $this->returnMsg(1, '修改成功', $db);
    }

    /**
     * 删除区域
     */
    public function delete_region()
    {
        $rid = input('id');
        $storehouses = Db::name('storehouse')->where(['region_id' => $rid, 'status' => 1])->find();
        if ($storehouses) {
            $this->returnMsg(-1, '已有仓库在当前区域，请先解除和对应仓库的关系');
        }

        $storehouses = Db::name('city')->where(['region_id' => $rid, 'status' => 1])->find();
        if ($storehouses) {
            $this->returnMsg(-1, '已有城市在当前区域，请先解除和对应城市关系');
        }


//        Db::name('region')->where(['id' => $rid])->delete();
        Db::name('region')->where(['id' => $rid])->update(['status' => 2]);
        $this->returnMsg(1, '删除成功');
    }


    /**
     * 地标点列表
     */
    public function landmark_list(){
        $db = Db::name('landmark')->select();
        foreach ($db as $key => $value) {
            $db[$key]['icon_url'] = to_all_url($value['icon_url']);
        }
        $this->returnMsg('1', '', $db);
    }

    /**
     * 地标点详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function landmark_msg()
    {
        $id = input('land_id',0);
        $db = Db::name('landmark')->where(['land_id'=>$id])->find();
        if ($db) {
            $db['icon_url'] = to_all_url($db['icon_url']);
        }
        $this->returnMsg('1', '', $db);
    }

    /**
     * 添加地标点
     */
    public function add_landmark()
    {
        $add_data = I('.');
        unset($add_data['token']);

        $rule = [
            'names' => 'require|length:1,50',
            'names_e' => 'require|length:2,50',
            'icon_url' => 'require|length:2,100',
        ];

        $msg = [
            'names.require' => '请输入地标名字',
            'names.length' => '请输入2-50字的地标名字',
            'names_e.require' => '请输入地标英文名字',
            'names_e.length' => '请输入2-100字的地标英文名字',
            'icon_url.require' => '请输入图标',
            'icon_url.length' => '输入图标过长',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($add_data);
        if (!$result) {
            $this->returnMsg(-1, $validate->getError());
        }

        $names = $add_data['names'];
        $have_db = Db::name('landmark')->where(['names' => $names])->find();
        if (!empty($have_db)) {
            $this->returnMsg(-1, '已有相同的地标', []);
        }

        $db = Db::name('landmark')->insert($add_data);
        $this->returnMsg(1, '添加成功', $db);
    }

    /**
     * 编辑地标
     */
    public function edit_landmark(){
        $add_data = I('.');
        unset($add_data['token']);

        $rule = [
            'land_id' => 'require|number',
            'names' => 'require|length:1,50',
            'names_e' => 'require|length:1,200',
            'icon_url' => 'require|length:1,200',
        ];

        $msg = [
            'land_id.require' => '请选择地标',
            'land_id.number' => '请选择地标编号',
            'names.require' => '请输入地标名字',
            'names.length' => '请输入1-50字的地标名字',
            'names_e.require' => '请输入地标英文名字',
            'names_e.length' => '请输入1-100字的地标英文名字',
            'icon_url.require' => '请输入图标',
            'icon_url.length' => '输入图标过长',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($add_data);
        if (!$result) {
            $this->returnMsg(-1, $validate->getError());
        }

        $lid = $add_data['land_id'];
        $names = $add_data['names'];
        $have_db = Db::name('landmark')->where(['names' => $names, 'land_id' => ['neq', $lid]])->find();
        if (!empty($have_db)) {
            $this->returnMsg(-1, '已有相同的地标名称', []);
        }

        $db = Db::name('landmark')->where(['land_id' => $lid])->update($add_data);
        $this->returnMsg(1, '修改成功', $db);
    }

    /**
     * 删除地标
     */
    public function delete_landmark()
    {
        $land_id = input('land_id');
        $storehouses_land = Db::name('storehouse_landmark')->where(['land_id' => $land_id])->find();
        if ($storehouses_land) {
            $this->returnMsg(-1, '已有仓库配置过当前地标，请先解除关系');
        }

        Db::name('landmark')->where(['land_id' => $land_id])->delete();
        $this->returnMsg(1, '删除成功');
    }
}